##########################################
#
# Copyright 2008 David B. Spadea
# All rights reserved
#
# http://www.spadea.net
#
# This program and all supporting code  
# is released under the terms of the 
# GNU General Public License version 3.
#
##########################################
package BatchTools::Calendar;

use strict;
use warnings;

use Data::Dumper;
use BatchTools::Database;

our $DB;
our $DATECARDS;

sub load
{
	my $class = shift;
	my %opts = @_;

	my $self = {};
	$DB = BatchTools::Database->new
		or die ("Unable to connect to database: " . $DBI::errstr);

	$DB->do("set search_path = cal, pg_catalog;");

	$$self{Name} = $opts{Name};

	# Don't really load anything yet. We should
	# probably validate the name, though. 
	
	return bless($self, $class);
}

sub curr_bus_dt
{
	my $self = shift;
	return $self->get_datecard('curr_bus_dt');
}

sub prev_bus_dt
{
	my $self = shift;
	return $self->get_datecard('prev_bus_dt');
}

sub next_bus_dt
{
	my $self = shift;
	return $self->get_datecard('next_bus_dt');
}

sub is_weekend
{
	my $self = shift;
	return $self->get_datecard('is_weekend');
}

sub is_business_date
{
	my $self = shift;
	return $self->get_datecard('is_business_date');
}

sub is_holiday
{
	my $self = shift;
	return $self->get_datecard('is_holiday');
}


sub load_datecards
{

	return 1 if ref($DATECARDS);

	my $sql = <<SQL;

	select * 
	from datecards

SQL

	$DATECARDS = $DB->selectall_hashref($sql, 'definition_name')
		or die($DBI::errstr);

	return 1;
}

sub get_datecard
{
	my $self = shift;
	my $field = shift;

	&load_datecards
		or return undef;

	return $$DATECARDS{ $$self{Name} }{$field};
	
}

1;
